<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>Registers a PHP function for use as an SQL aggregate function</title>
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-base.css" />
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-medium.css" />

 </head>
 <body class="docs"><div class="navbar navbar-fixed-top">
  <div class="navbar-inner clearfix">
    <ul class="nav" style="width: 100%">
      <li style="float: left;"><a href="sqlite3.construct.html">« SQLite3::__construct</a></li>
      <li style="float: right;"><a href="sqlite3.createcollation.html">SQLite3::createCollation »</a></li>
    </ul>
  </div>
</div>
<div id="breadcrumbs" class="clearfix">
  <ul class="breadcrumbs-container">
    <li><a href="index.html">PHP Manual</a></li>
    <li><a href="class.sqlite3.html">SQLite3</a></li>
    <li>Registers a PHP function for use as an SQL aggregate function</li>
  </ul>
</div>
<div id="layout">
  <div id="layout-content"><div id="sqlite3.createaggregate" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">SQLite3::createAggregate</h1>
  <p class="verinfo">(PHP 5 &gt;= 5.3.0, PHP 7, PHP 8)</p><p class="refpurpose"><span class="refname">SQLite3::createAggregate</span> &mdash; <span class="dc-title">Registers a PHP function for use as an SQL aggregate function</span></p>

 </div>

 <div class="refsect1 description" id="refsect1-sqlite3.createaggregate-description">
  <h3 class="title">说明</h3>
  <div class="methodsynopsis dc-description">
   <span class="modifier">public</span> <span class="methodname"><strong>SQLite3::createAggregate</strong></span>(<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="methodparam"><span class="type">string</span> <code class="parameter">$name</code></span>,<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="methodparam"><span class="type"><a href="language.types.callable.html" class="type callable">callable</a></span> <code class="parameter">$stepCallback</code></span>,<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="methodparam"><span class="type"><a href="language.types.callable.html" class="type callable">callable</a></span> <code class="parameter">$finalCallback</code></span>,<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="methodparam"><span class="type">int</span> <code class="parameter">$argCount</code><span class="initializer"> = -1</span></span><br>): <span class="type">bool</span></div>

  <p class="para rdfs-comment">
   Registers a PHP function or user-defined function for use as an SQL
   aggregate function for use within SQL statements.
  </p>
 </div>


 <div class="refsect1 parameters" id="refsect1-sqlite3.createaggregate-parameters">
  <h3 class="title">参数</h3>
  <p class="para">
   <dl>
    
     <dt>
<code class="parameter">name</code></dt>

     <dd>

      <p class="para">
       Name of the SQL aggregate to be created or redefined.
      </p>
     </dd>

    
    
     <dt>
<code class="parameter">stepCallback</code></dt>

     <dd>

      <p class="para">
       Callback function called for each row of the result set. Your PHP
       function should accumulate the result and store it in the aggregation
       context.
      </p>
      <p class="para">
       This function need to be defined as:
       <div class="methodsynopsis dc-description">
        <span class="methodname"><span class="replaceable">step</span></span>(<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="methodparam"><span class="type"><a href="language.types.declarations.html#language.types.declarations.mixed" class="type mixed">mixed</a></span> <code class="parameter">$context</code></span>,<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="methodparam"><span class="type">int</span> <code class="parameter">$rownumber</code></span>,<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="methodparam"><span class="type"><a href="language.types.declarations.html#language.types.declarations.mixed" class="type mixed">mixed</a></span> <code class="parameter">$value</code></span>,<br>&nbsp;&nbsp;&nbsp;&nbsp;<span class="methodparam"><span class="type"><a href="language.types.declarations.html#language.types.declarations.mixed" class="type mixed">mixed</a></span> <code class="parameter">...$values</code></span><br>): <span class="type"><a href="language.types.declarations.html#language.types.declarations.mixed" class="type mixed">mixed</a></span></div>

       <dl>
        
         <dt>
<code class="parameter">context</code></dt>

         <dd>

          <p class="para">
           <strong><code>null</code></strong> for the first row; on subsequent rows it will have the value
           that was previously returned from the step function; you should use
           this to maintain the aggregate state.
          </p>
         </dd>

        
        
         <dt>
<code class="parameter">rownumber</code></dt>

         <dd>

          <p class="para">
           The current row number.
          </p>
         </dd>

        
        
         <dt>
<code class="parameter">value</code></dt>

         <dd>

          <p class="para">
           The first argument passed to the aggregate.
          </p>
         </dd>

        
        
         <dt>
<code class="parameter">values</code></dt>

         <dd>

          <p class="para">
           Further arguments passed to the aggregate.
          </p>
         </dd>

        
       </dl>

       The return value of this function will be used as the
       <code class="parameter">context</code> argument in the next call of the step or
       finalize functions.
      </p> 
     </dd>

    
    
     <dt>
<code class="parameter">finalCallback</code></dt>

     <dd>

      <p class="para">
       Callback function to aggregate the &quot;stepped&quot; data from each row. 
       Once all the rows have been processed, this function will be called
       and it should then take the data from the aggregation context and
       return the result. This callback function should return a type understood
       by SQLite (i.e. <a href="language.types.intro.html" class="link">scalar type</a>).
      </p>
      <p class="para">
       This function need to be defined as:
       <div class="methodsynopsis dc-description">
        <span class="methodname"><span class="replaceable">fini</span></span>(<span class="methodparam"><span class="type"><a href="language.types.declarations.html#language.types.declarations.mixed" class="type mixed">mixed</a></span> <code class="parameter">$context</code></span>, <span class="methodparam"><span class="type">int</span> <code class="parameter">$rownumber</code></span>): <span class="type"><a href="language.types.declarations.html#language.types.declarations.mixed" class="type mixed">mixed</a></span></div>

       <dl>
        
         <dt>
<code class="parameter">context</code></dt>

         <dd>

          <p class="para">
           Holds the return value from the very last call to the step function.
          </p>
         </dd>

        
        
         <dt>
<code class="parameter">rownumber</code></dt>

         <dd>

          <p class="para">
           Always <code class="literal">0</code>.
          </p>
         </dd>

        
       </dl>

       The return value of this function will be used as the return value for
       the aggregate.
      </p>
     </dd>

    
    
     <dt>
<code class="parameter">argCount</code></dt>

     <dd>

      <p class="para">
       The number of arguments that the SQL aggregate takes. If
       this parameter is negative, then the SQL aggregate may take
       any number of arguments.
      </p>
     </dd>

    
   </dl>

  </p>
 </div>


 <div class="refsect1 returnvalues" id="refsect1-sqlite3.createaggregate-returnvalues">
  <h3 class="title">返回值</h3>
  <p class="para">
   Returns <strong><code>true</code></strong> upon successful creation of the aggregate,  或者在失败时返回 <strong><code>false</code></strong>.
  </p>
 </div>


 <div class="refsect1 examples" id="refsect1-sqlite3.createaggregate-examples">
  <h3 class="title">范例</h3>
  <p class="para">
   <div class="example" id="example-1942">
    <p><strong>示例 #1 max_length aggregation function example</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br />$data&nbsp;</span><span style="color: #007700">=&nbsp;array(<br />&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">'one'</span><span style="color: #007700">,<br />&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">'two'</span><span style="color: #007700">,<br />&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">'three'</span><span style="color: #007700">,<br />&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">'four'</span><span style="color: #007700">,<br />&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">'five'</span><span style="color: #007700">,<br />&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">'six'</span><span style="color: #007700">,<br />&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">'seven'</span><span style="color: #007700">,<br />&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">'eight'</span><span style="color: #007700">,<br />&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">'nine'</span><span style="color: #007700">,<br />&nbsp;&nbsp;&nbsp;</span><span style="color: #DD0000">'ten'</span><span style="color: #007700">,<br />&nbsp;&nbsp;&nbsp;);<br /></span><span style="color: #0000BB">$db&nbsp;</span><span style="color: #007700">=&nbsp;new&nbsp;</span><span style="color: #0000BB">SQLite3</span><span style="color: #007700">(</span><span style="color: #DD0000">':memory:'</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$db</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">exec</span><span style="color: #007700">(</span><span style="color: #DD0000">"CREATE&nbsp;TABLE&nbsp;strings(a)"</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$insert&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">$db</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">prepare</span><span style="color: #007700">(</span><span style="color: #DD0000">'INSERT&nbsp;INTO&nbsp;strings&nbsp;VALUES&nbsp;(?)'</span><span style="color: #007700">);<br />foreach&nbsp;(</span><span style="color: #0000BB">$data&nbsp;</span><span style="color: #007700">as&nbsp;</span><span style="color: #0000BB">$str</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$insert</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">bindValue</span><span style="color: #007700">(</span><span style="color: #0000BB">1</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$str</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$insert</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">execute</span><span style="color: #007700">();<br />}<br /></span><span style="color: #0000BB">$insert&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">null</span><span style="color: #007700">;<br /><br />function&nbsp;</span><span style="color: #0000BB">max_len_step</span><span style="color: #007700">(</span><span style="color: #0000BB">$context</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$rownumber</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$string</span><span style="color: #007700">)<br />{<br />&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(</span><span style="color: #0000BB">strlen</span><span style="color: #007700">(</span><span style="color: #0000BB">$string</span><span style="color: #007700">)&nbsp;&gt;&nbsp;</span><span style="color: #0000BB">$context</span><span style="color: #007700">)&nbsp;{<br />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: #0000BB">$context&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">strlen</span><span style="color: #007700">(</span><span style="color: #0000BB">$string</span><span style="color: #007700">);<br />&nbsp;&nbsp;&nbsp;&nbsp;}<br />&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;</span><span style="color: #0000BB">$context</span><span style="color: #007700">;<br />}<br /><br />function&nbsp;</span><span style="color: #0000BB">max_len_finalize</span><span style="color: #007700">(</span><span style="color: #0000BB">$context</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$rownumber</span><span style="color: #007700">)<br />{<br />&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;</span><span style="color: #0000BB">$context&nbsp;</span><span style="color: #007700">===&nbsp;</span><span style="color: #0000BB">null&nbsp;</span><span style="color: #007700">?&nbsp;</span><span style="color: #0000BB">0&nbsp;</span><span style="color: #007700">:&nbsp;</span><span style="color: #0000BB">$context</span><span style="color: #007700">;<br />}<br /><br /></span><span style="color: #0000BB">$db</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">createAggregate</span><span style="color: #007700">(</span><span style="color: #DD0000">'max_len'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'max_len_step'</span><span style="color: #007700">,&nbsp;</span><span style="color: #DD0000">'max_len_finalize'</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$db</span><span style="color: #007700">-&gt;</span><span style="color: #0000BB">querySingle</span><span style="color: #007700">(</span><span style="color: #DD0000">'SELECT&nbsp;max_len(a)&nbsp;from&nbsp;strings'</span><span style="color: #007700">));<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

    <div class="example-contents"><p>以上例程会输出：</p></div>
    <div class="example-contents screen">
<div class="cdata"><pre>
int(5)
</pre></div>
    </div>
   </div>
  </p>
  <p class="para">
   In this example, we are creating an aggregating function that will
   calculate the length of the longest string in one of the columns of the
   table.  For each row, the <code class="literal">max_len_step</code> function is
   called and passed a <code class="literal">$context</code> parameter.  The context
   parameter is just like any other PHP variable and be set to hold an array
   or even an object value.  In this example, we are simply using it to hold
   the maximum length we have seen so far; if the
   <code class="literal">$string</code> has a length longer than the current
   maximum, we update the context to hold this new maximum length.
  </p>
  <p class="para">
   After all of the rows have been processed, SQLite calls the
   <code class="literal">max_len_finalize</code> function to determine the aggregate
   result.  Here, we could perform some kind of calculation based on the
   data found in the <code class="literal">$context</code>.  In our simple example
   though, we have been calculating the result as the query progressed, so we
   simply need to return the context value.
  </p>
  <div class="tip"><strong class="tip">小技巧</strong>
   <p class="para">
    It is NOT recommended for you to store a copy of the values in the context
    and then process them at the end, as you would cause SQLite to use a lot of
    memory to process the query - just think of how much memory you would need
    if a million rows were stored in memory, each containing a string 32 bytes
    in length.
   </p>
  </div>
  <div class="tip"><strong class="tip">小技巧</strong>
   <p class="para">
    You can use <span class="methodname"><strong>SQLite3::createAggregate()</strong></span> to override SQLite
    native SQL functions.
   </p>
  </div>
 </div>


</div></div></div></body></html>